package com.kb.design.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kb.design.common.ApiResponse;
import com.kb.design.common.ResponseObject;
import com.kb.design.dto.BonusConfigDTO;
import com.kb.design.entity.BonusConfig;
import com.kb.design.entity.CustomUser;
import com.kb.design.service.BonusConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/bonusConfig")
public class BonusConfigController {

    @Autowired
    private BonusConfigService bonusConfigService;

    /**
     * 根据BonusConfig对象分页查询绩效配置列表，并进行LEFT JOIN design_post表
     *
     * @param
     * @param bonusConfig 查询条件
     * @return 分页后的绩效配置列表
     */
    @PostMapping("/pageList")
    public ApiResponse getBonusConfigByConditionWithJoin(@RequestBody BonusConfigDTO bonusConfig) {
        Page<BonusConfig> page = new Page<>(bonusConfig.getPage(), bonusConfig.getPerPage());
        ResponseObject responseObject = new ResponseObject();

        IPage<BonusConfig> bonusConfigByCondition = bonusConfigService.getBonusConfigByCondition(page, bonusConfig);
        responseObject.setItems(bonusConfigByCondition.getRecords());
        responseObject.setTotal(bonusConfigByCondition.getTotal());
        return ApiResponse.success(responseObject);
    }
    /**
     * 新增绩效配置
     *
     * @param bonusConfig 新增的绩效配置对象
     * @return 新增的绩效配置对象
     */
    @PostMapping("/saveBonusConfig")
    public ApiResponse saveBonusConfig(@RequestBody BonusConfig bonusConfig) {
        return ApiResponse.success(bonusConfigService.saveBonusConfig(bonusConfig));
    }

    /**
     * 修改绩效配置
     *
     * @param bonusConfig 修改的绩效配置对象
     * @return 是否修改成功
     */
    @PostMapping("updateBonusConfig")
    public ApiResponse updateBonusConfig(@RequestBody BonusConfig bonusConfig) {
        return ApiResponse.success(bonusConfigService.updateBonusConfig(bonusConfig));
    }

    /**
     * 根据ID查询绩效配置详情
     *
     * @param id 绩效配置ID
     * @return 绩效配置对象
     */
    @GetMapping("getBonusConfigById")
    public ApiResponse getBonusConfigById( Long id) {
        return ApiResponse.success(bonusConfigService.getBonusConfigById(id));
    }
    /**
     * 删除
     *
     * @param id 绩效配置ID
     * @return 绩效配置对象
     */
    @PostMapping("deleteById")
    public ApiResponse deleteById(Long id) {
        return ApiResponse.success(bonusConfigService.deleteById(id));
    }
}
